home *** CD-ROM | disk | FTP | other *** search
- :
- #
- # E-Mail adddress directory lookup script to read the passwd file
- #
- if test $# -ne 2
- then
- echo "`basename $0`: Expected 2 args, got $#"
- exit 3 # Incorrect arguments
- fi
- matches=${TMPDIR-/tmp}/pwmatch$$
- domainname=`(domainname) 2>/dev/null`
- if test -n "$domainname"
- then
- domainname="-d $domainname"
- else
- hostname=`( hostname || uname -n || uuname -l ) 2>/dev/null`
- case "$hostname" in
- *.*) domainname="-d $hostname";;
- esac
- fi
-
- # make SP = space + tab
- SP=`echo x | tr x '\011'``echo x | tr x '\040'`
-
- # extract address part if this doesn't look like it's already a regex
- # this won't work absolutely 100% but it should get it right on
- # non-perverse cases
- addressonly=$2
- if echo "$addressonly" | grep -v '[]\*]' >/dev/null
- then
- # the following sed expressions do this, in order:
- # - assume that anything in double quotes is a comment and drop it
- # - pull out the address from any remaining comments
- # - squeeze adjacent whitespace, strip leading/trailing whitespace
- # - drop anything that looks like a relay/host name; in particular
- # user@host, host!user@relay, user%host@relay, host1!host2!...!user
- addressonly=`echo " $addressonly " \
- | sed -e 's/\([^!]\)"[^"]*"\([^@]\)/\1\2/g' \
- -e 's/([^)]*)//g' -e 's/^.*<\([^>]*\)>.*$/\1/' \
- -e "s/[$SP][$SP]*/ /g" -e "s/^[$SP]//" -e "s/[$SP]$//" \
- -e 's/@.*$//' -e 's/%.*$//' -e 's/^.*!\([^!]*\)$/\1/'`
-
- # if we somehow nuked the entire string, fall back on the original
- # string, stripping one level of quoting if it exists
- test -z "$addressonly" && addressonly=`echo "$2" \
- | sed -e 's/^"\(.*\)"$/\1/'`
- fi
-
- getpasswd="(ypcat $domainname passwd || cat /etc/passwd) 2>/dev/null"
- eval "$getpasswd" | cut -d: -f1,5 | grep "$addressonly" > $matches
- count=`wc -l < $matches`
- EXIT=1 # Execution failure
- if test $count -eq 0
- then
- echo "$2"
- EXIT=4 # No matches found
- elif test $count -gt $1 -a $1 -gt 0
- then
- echo "Matched $count names (max $1)"
- echo "Use a more specific pattern please."
- EXIT=2 # Too many matches
- else
- sed 's/\([^:]*\):\([^,]*\),.*$/\1 (\2)/' < $matches
- if test $count -eq 1
- then
- EXIT=5 # Exactly one match
- else
- EXIT=0 # At least one match
- fi
- fi
- rm -f $matches
- exit $EXIT
-